

function searchBing(query, types, handler, page)
{
	if(!page)
	{
		page = 0;
	}
	
	var sources = '';
	if(!(types instanceof Array))
	{
		types = new Array(types);
	}
	for(var i = 0; i < types.length; i++)
	{
		$('result_loader_' + types[i]).style.display = '';
		
		if (types[i] == 'Translation') {
			sources += '&Sources=' + types[i] +
			'&' + types[i] + '.SourceLanguage=It' +
			'&' + types[i] + '.TargetLanguage=Fi';	// make this dynamic, one day
		}
		else {
			sources += '&Sources=' + types[i] +
			'&' + types[i] + '.Count=' + searchPerPage +
			'&' + types[i] + '.Offset=' + (page * searchPerPage);
		}	
	}
	
	var url = "http://api.search.live.net/json.aspx?AppId=F71F60E4B8C091B1D141A890ADD5E330B0C74EAE&" + 
		"Query=" + query + 
		sources
		
	var typesHandler = function(result)
	{
		handler(result, types);
	}
	loadJSON(url, typesHandler);
}


function loadJSON(url, handler)
{
	try
	{
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	}
	catch(e)
	{
		
	}
		
	
	try
	{
		var request = new XMLHttpRequest();
		request.open("GET", url, true );
		request.send(null);

		request.onreadystatechange = function(){

			if(request.readyState == 4)
			{
				if(request.status == 200)
				{
					eval("var res = " + request.responseText);
					
					handler(res);
				}
				else
				{
					handler(false);
				}
			}
		}
	}
	catch( e )
	{
		handler(false);
	}
}
